package com.example.contentobserverdb;

import static java.security.AccessController.getContext;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ContentResolver resolver;
    private Uri uri;
    private ContentValues values;
    private Button btnInsert;
    private Button btnUpdate;
    private Button btnDelete;
    private Button btnSelect;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        initView();
        createDB();
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
    }

    private void initView(){
        btnInsert = findViewById(R.id.btnInsert);
        btnUpdate = findViewById(R.id.btnUpdate);
        btnDelete = findViewById(R.id.btnDelete);
        btnSelect = findViewById(R.id.btnSelect);

        btnInsert.setOnClickListener(this);
        btnUpdate.setOnClickListener(this);
        btnDelete.setOnClickListener(this);
        btnSelect.setOnClickListener(this);
    }
    private void createDB() {
        PersonDBOpenHelper helper = new PersonDBOpenHelper(this);
        SQLiteDatabase db = helper.getWritableDatabase();
        for (int i = 0; i < 3; i++) {
            ContentValues values = new ContentValues();
            values.put("name","itcast"+1);
            db.insert("info",null,values);
        }
        db.close();
    }

    @Override
    public void onClick(View v){
        resolver = getContentResolver();
        uri = Uri.parse("content://com.example.contentobserverdb/info");
        values = new ContentValues();
        if (v.getId() == R.id.btnInsert) {
            Random random = new Random();
            values.put("name","add_itcast"+random.nextInt(10));
            Uri newUri = resolver.insert(uri,values);

            Toast.makeText(this,"添加成功",Toast.LENGTH_SHORT).show();
        } else if(v.getId() == R.id.btnDelete){
            int deleteCoun = resolver.delete(uri,"name=?",new String[]{"update_itcast"});
            Toast.makeText(this,"成功删除了"+deleteCoun+"行",Toast.LENGTH_SHORT).show();
            Log.i("数据库应用", "删除");
        } else if (v.getId() == R.id.btnSelect) {
            List<Map<String,String>> data = new ArrayList<Map<String,String>>();
            Cursor cursor = resolver.query(uri,new String[]{"_id","name"},null,null,null);

            while (cursor.moveToNext()){
                Map<String,String> map = new HashMap<String,String>();
                map.put("_id",cursor.getString(0));
                map.put("name",cursor.getString(1));
                data.add(map);
            }
            cursor.close();
            Log.i("数据库", "查询结果："+data.toString());

        } else if (v.getId() == R.id.btnUpdate) {
            values.put("name","update_itcast");
            int upDteCount = resolver.update(uri,values,"name=?",new String[]{"itcast1"});
            Toast.makeText(this,"成功更新了"+upDteCount+"行",Toast.LENGTH_SHORT).show();
            Log.i("数据库", "更新");
        }

    }
}